package com.huicoo.forestmanager.util;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.huicoo.forestmanager.util.toast.ToastUtils;

public class SystemFunctionUtils {

    /**
     * 拨打电话
     */
    public static void dial(Activity activity, String phone) {
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE)) {
            ToastUtils.show("需要拨打电话权限，在授权后重试");
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE}, 502);
        } else {
            try {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel://" + phone));
                activity.startActivity(intent);
            } catch (Exception e) {
                ToastUtils.show("找不到可以拨打电话的应用程序");
                e.printStackTrace();
            }
        }
    }
}
